<template>
  <view class="content goods-content">
    <view class="goods-main">
      <view class="goods-right">
        <view class="type-title">购物车</view>
        <view class="goods-list-panel">
          <view 
            class="goods-list"
			:class="[]"
            v-for="(item, index) in dataList" 
            :key="index"
            @touchstart="touchStart($event, index)"
            @touchmove="touchMove($event, index)"
            @touchend="touchEnd(index)"
            :style="{transform: `translateX(${item.offsetX}px)`}">
            
            <view class="goods-img-box">
              <image class="goods-img" src="/static/logo.png" mode="aspectFill"></image>
            </view>
            
            <view class="goods-info">
              <view class="goods-title no-warp">{{item.title}}</view>
              <view class="goods-price">¥ {{item.price}}</view>
              <view class="goods-sell">{{item.sellCount}}</view>
              <view class="goods-buy">
                <text class="iconfont">&#xe711;</text>
                <input class="buy-number" v-model="item.count"/>
                <text class="iconfont">&#xe607;</text>
              </view>
            </view>
			<view class="delete-btn" @click.stop="deleteItem(index)">删除</view>
          </view>
        </view>
      </view>
    </view>
    <view class="bottom-box">
      <view class="iconfont">&#xe646;</view>
      <view class="play-info">
        <view class="play-tips">应付:</view>
        <view>
          <text class="play-num">¥ {{totalPrice.toFixed(2)}}</text>
          <text class="play-count">（{{totalCount}}件）</text>
        </view>
      </view>
      <view class="bottom-btn">立即支付</view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { computed, reactive } from 'vue'

interface CartItem {
  title: string
  price: string
  sellCount: string
  count: number
  offsetX: number
}

const dataList = reactive<CartItem[]>([
  {
    title: '姜醒植物饮料',
    price: '20.00',
    sellCount: '已售100+',
    count: 1,
    offsetX: 0
  },
  {
    title: '姜醒植物饮料2',
    price: '20.00',
    sellCount: '已售100+',
    count: 1,
    offsetX: 0
  },
])

const totalPrice = computed(() => {
  return dataList.reduce((sum, item) => sum + (parseFloat(item.price) * item.count), 0)
})

const totalCount = computed(() => {
  return dataList.reduce((sum, item) => sum + item.count, 0)
})

const deleteWidth = 80
let startX = 0
let currentIndex = -1

const touchStart = (e: TouchEvent, index: number) => {
  startX = e.touches[0].clientX
  currentIndex = index
}

const touchMove = (e: TouchEvent, index: number) => {
  if (currentIndex !== index) return
  
  const moveX = e.touches[0].clientX
  const diff = moveX - startX
  
  if (Math.abs(diff) <= deleteWidth) {
    dataList[index].offsetX = diff
  }
}

const touchEnd = (index: number) => {
  const threshold = deleteWidth / 2
  if (dataList[index].offsetX < -threshold) {
    dataList[index].offsetX = -deleteWidth
  } else {
    dataList[index].offsetX = 0
  }
  currentIndex = -1
}

const resetSwipe = (index: number) => {
  dataList[index].offsetX = 0
}
const deleteItem = (index: number) => {
  dataList.splice(index, 1)
}
</script>

<style lang="scss">
	.delete-btn{
		position: absolute;
		right: -160px;
		opacity: 1;
		writing-mode: vertical-rl; /* 从右向左竖排 */
	    text-orientation: upright; /* 保持字符直立 */
		letter-spacing: 20rpx;
		width: 150rpx;
		height: 204rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #FFFFFF;
	}
	.goods-list[style*="translateX(-80px)"] .delete-btn {
		transition: all 0.7s;
		opacity: 1;
		background-color: #ff3134;
		right: -80px;
	}
	.goods-content {
		.goods-main {
			
		}
		.goods-right{
			margin: 0;
			padding: 0;
			box-sizing: border-box;
			width: 100%;
			display: inline-block;
			padding: 0 15rpx;
			height: 100%;
		}
		
		.goods-type{
			height: 66rpx;
			width: 140rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-bottom: solid 1px #ddd;
			background: #e8e8e8;
			font-weight: 500;
			font-size: 24rpx;
			color: #1E1E1E;
		}
		.goods-type-active{
			background-color: #fff;
		}
		.type-title{
			height: 66rpx;
			display: flex;
			align-items: center;
			font-weight: 500;
			font-size: 28rpx;
			color: #1E1E1E;
		}
		.goods-list{
			margin: 0;
			padding: 0;
			box-sizing: border-box;
			display: inline-flex;
			align-items: center;
			width: 100%;
			background: #ffffff;
			box-shadow: 0rpx 1rpx 4rpx 1rpx rgba(0,0,0,0.3);
			border-radius: 8rpx;
			margin-bottom: 10rpx;
			padding: 10rpx;
			&:last-of-type{
				margin-bottom: 110rpx;
			}
		}
		
		.goods-info{
			padding-left: 20rpx;
			flex-grow: 1;
			width: 100rpx;
		}
		.goods-img-box{
			width: 180rpx;
			height: 180rpx;
			background: #FFFFFF;
			border-radius: 3rpx;
			border: 1rpx solid rgba(16,53,151,0.28);
			flex-shrink: 0;
		}
		.goods-img{
			width: 100%;
			height: 100%;
		}
		
		.goods-title{
			font-weight: 500;
			font-size: 26rpx;
			color: #1E1E1E;
		}
		.goods-price{
			font-weight: 500;
			font-size: 24rpx;
			color: #FB1E16;
			margin-top: 5rpx;
		}
		.goods-sell{
			font-size: 24rpx;
			color: #1E1E1E;
			margin-top: 5rpx;
		}
		.goods-buy{
			display: flex;
			align-items: center;
			margin-top: 5rpx;
			justify-content: flex-end;
			margin-right: 10rpx;
			.iconfont{
				font-size: 24rpx;
				font-weight: bold;
				color: #A1A1A1;
			}
		}
		.buy-number{
			width: 50rpx;
			font-size: 28rpx;
			color: #1E1E1E;
			text-align: center;
			border: solid 1px rgba(16,53,151,0.2);
			margin: 0 15rpx;
		}
		
		
		.bottom-box{
			margin: 0;
			padding: 0;
			box-sizing: border-box;
			position: fixed;
			height: 105rpx;
			width: 100%;
			bottom: 0rpx;
			left: 0;
			background-color: #fff;
			box-shadow: 0rpx -2rpx 3rpx 0rpx rgba(0,0,0,0.08);
			display: flex;
			align-items: center;
			padding: 0 6rpx;
			.iconfont{
				padding-right: 20rpx;
				font-size: 50rpx;
				font-weight: bold;
				color: #2F65F3;
			}
		}
		.play-info{
			flex-grow: 1;
		}
		.play-tips{
			font-size: 24rpx;
			color: #4E4E4E;
		}
		.play-num{
			font-weight: 500;
			font-size: 32rpx;
			color: #FB1E16;
		}
		.play-count{
			font-size: 24rpx;
			color: #4E4E4E;
		}
		.bottom-btn{
			margin: 0 5rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #00e16d;
			
			font-weight: 500;
			font-size: 26rpx;
			color: #FFFFFF;
			height: 70rpx;
			border-radius: 8rpx;
			width: 180rpx;
		}
	}
</style>